package xupt.com.bolg.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import xupt.com.bolg.entity.Bolg;
import xupt.com.bolg.entity.User;
import xupt.com.bolg.service.impl.BolgServiceImpl;
import xupt.com.bolg.service.impl.UserServiceImpl;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class LoginController {
    @Autowired
    BolgServiceImpl bolgService;

    @Autowired
    UserServiceImpl userService;
    @GetMapping("/")
    public String skip(HttpServletRequest request){
      return "index";
    }
    @PostMapping("/login")
    public String login(@RequestParam(value = "name")String name, @RequestParam("password")String password,
                       HttpServletRequest request,Model model, HttpServletResponse response){
        User byName = userService.findByName(name);
        if(byName.getPassword().equals(password)){
            //session 中写入数据
            request.getSession().setAttribute("name",name);
            List<Bolg> all = bolgService.findAll();
            System.out.println(all.size());
            model.addAttribute("bolgs",all);
            return "redirect:/main.html";
         }
         request.setAttribute("msg","账号或密码错误");
         return "index";
    }
}
